﻿#pragma once

#include "MBTileReader.h"
#include "VectorTile.h"
#include "OCMain/ZipUtil.h"
#pragma warning(disable: 4068 5232)
#include "mapbox/geojson.hpp"
//#include "mapbox/geojson_value_impl.hpp"
#include "mapbox/geojsonvt.hpp"
#include "mapbox/util.hpp"
#include "mapbox/geojsonvt_io.hpp"
#include "mapbox/geojsonvt_ioImp.hpp"
#include "OCesium/TilingScheme.h"

using namespace OCPP;
using namespace OCPP::Cesium;

class ExampleDataStore : public DecodeVectorTileResults
{
public:
	ExampleDataStore() : DecodeVectorTileResults()
	{
		OCPP_LOG_INFO << "Create custom data store...\n";
	}

	void Feature(int typeEnum, bool hasId, unsigned long long id,
		const std::map<std::string, std::string>& tagMap,
		std::vector<Point2D>& pointsOut,
		std::vector<std::vector<Point2D> >& linesOut,
		std::vector<Polygon2D>& polygonsOut)
	{
		//In real use, delete this function call and add your own functionality.
		DecodeVectorTileResults::Feature(typeEnum, hasId, id,
			tagMap, pointsOut, linesOut, polygonsOut);
	}
};

void readPBF();
void testGeosjonVt();

void testReadPBF()
{
	readPBF();
}

using namespace mapbox;

void testGeosjonVt()
{
	OCPP_LOG_INFO << "testReadPBF\n";
	String fileName1 = "e:/test/wh.json";
	String content = FileUtil::getFileText(fileName1);

	try {
		const auto geojson = geojson::parse(content);

		mapbox::geojsonvt::Options options;
		options.maxZoom = 22;
		options.indexMaxZoom = 18;
		options.extent = 8192;
		options.tolerance = 2;
		options.tolerance *= 4;
		//options.indexMaxPoints = 100000;
		
		geojsonvt::GeoJSONVT index{ geojson, options };

		String output = "F:\\opt\\data\\Assets\\maptiles\\wuhan";
		std::shared_ptr<TilingScheme> tilingScheme = std::make_shared<WebMercatorTilingScheme>();

		int startX = 0, startY = 0;
		for (int z = 1; z <= 9; z++)
		{
			int numX = tilingScheme->getNumberOfXTilesAtLevel(z);
			int numY = tilingScheme->getNumberOfYTilesAtLevel(z);
			int y, x;
			startX *= 2;
			startY *= 2;
			int count = 0;
			int tmpX, tmpY;
			for (y = startY; y < numY; y++)
			{
				for (x = startX; x < numX; x++)
				{
					String path = Stringify() << output << "/" << z << "/" << y;
					String fileName = Stringify() << path << "/" << x << ".json";
					//if (FileUtil::existFile(fileName)) continue;
					auto& tile = index.getTile(z, x, y);
					if (tile.features.size() == 0 || tile.num_points == 0) {
						continue;
					}
					FileUtil::makeDirectory(path);
					FileUtil::writeFile(fileName, geojsonvt::stringify(geojsonvt::geojson_vt{ tile.features }));
					OCPP_LOG_INFO << fileName << "\n";
					count++;
					tmpX = x;
					tmpY = y;
				}
			}
			if (count == 1)
			{
				startX = tmpX;
				startY = tmpY;
			}
		}
	}
	catch (std::runtime_error& ex) {
		OCPP_LOG_ERROR << ex.what() << "\n";
	}
}

void readPBF()
{
	GOOGLE_PROTOBUF_VERIFY_VERSION;
	String fileName = "e:/test/pbf/6_4.mvt";

	int z = 10;
	int y = 6;
	int x = 4;


	//String fileName = Stringify() << "F:/World_Basemap_v2/" << z << "/" <<
	//	y << "/" << x << ".pbf";

	String tileData = FileUtil::getFileBinary(fileName);

	//tileData = ZipUtil::gzipDecompress(tileData);


	class ExampleDataStore results;
	class DecodeVectorTile vectorDec(z, x, y, results);
	vectorDec.DecodeTileData(tileData);

	google::protobuf::ShutdownProtobufLibrary();
}
